<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Common\Controller;
use Think\Controller;

/**
 * 控制器基础类
 *
 * @author 薛鹏
 */
class BaseController extends Controller {
    /**
    * json输入流参数基础验证
    * @param array $method
    */
   protected function paramValidate(array $method)
   {
       $paramArr = inputJson();
       $needed = '';
       foreach ($method as $Key => $param) {
           $data = $this->validateValue($paramArr[$Key], $param);
           if (!empty($Key) && !$data['pass']) {
               $needed .= !empty($needed) ? ", ".$Key.":".$data['msg'] : $Key.":".$data['msg'];
           }
       }
       if (!empty($needed))
           errorOutputJson('无效参数: '.$needed, false);
   }

   private function validateValue($value, $validates)
   {
       if (is_array($validates)){
           return ['pass' => in_array($value, $validates), 'msg' => ''];
       }
       else {
           $pass = true;
           $msg = '';
           $validates = explode(",", $validates);
          foreach ($validates as $Validate) {
              $extend = '';
              if (strpos($Validate, ":") === false)
                  $Type = $Validate;
              else
                  list($Type, $extend) = explode(":", $Validate, 2);
              switch ($Type) {
                  case 'number':
                      $pass = is_numeric($value);
                      break;

                  case 'number+':
                      $pass = is_numeric($value) && !empty($value);
                      break;
                  case 'must':
                      $pass = $value === null ? false : true;
                      break;
                  case 'email':
                      $pass = (boolean) filter_var($value, FILTER_VALIDATE_EMAIL);
                      break;
                 case 'min':
                      $extend = (int) $extend;
                      if (!empty($extend)) {
                          if (is_array($value) || is_object($value))
                              $pass = count($value) >= $extend ? true : false;
                          else
                              $pass = strlen($value) >= $extend ? true : false;
                          if (!$pass)
                              $msg = '参数太短';
                      }
                      break;

                  case 'max':
                      $extend = (int) $extend;
                      if (!empty($extend)) {
                          if (is_array($value) || is_object($value))
                              $pass = count($value) <= $extend ? true : false;
                          else
                              $pass = strlen($value) <= $extend ? true : false;
                          if (!$pass)
                              $msg = '参数太长';
                      }
                      break;

                   case 'len':
                        $extend = (int) $extend;
                        if (!empty($extend)) {
                            if (is_array($value) || is_object($value))
                                $pass = count($value) === $extend ? true : false;
                            else
                                $pass = strlen($value) === $extend ? true : false;
                        }
                        break;

                  case 'alphabet':
                      switch ($extend) {

                          case 'upper':
                              $pass = ctype_upper($value);
                              break;

                          case 'lower':
                              $pass = ctype_lower($value);
                              break;

                          default:
                              $pass = ctype_alpha($value);
                      }
                      break;

                  case 'alphanumber':
                      $pass = ctype_alnum($value);
                      break;

                  case 'ip':
                      switch ($extend) {
                          case 'v4':
                              $pass = (boolean) filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
                              break;

                          case 'v6':
                              $pass = (boolean) filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
                              break;

                          default:
                              $pass = (boolean) filter_var($value, FILTER_VALIDATE_IP);
                      }
                      break;

                  default:
                      if (is_array($Validate))
                          $pass = in_array($value, $Validate);
                      else
                          $pass = (boolean) preg_match($Validate, $value);
              }
              if (!$pass)
                  return ['pass' => $pass, 'msg' => $msg];
          }
           return ['pass' => true, 'msg' => $msg];
       }
   }
}
